<template>
  <div class="login">
    <el-card class="login_card">
      <h1 class="login_title">后台管理</h1>
      <el-form :model="loginInfo" ref="loginInfo" label-width="100px" class="login_form" :rules="rules">
        <el-form-item prop="account" label="账号">
          <el-input v-model="loginInfo.account" placeholder="请输入账号"></el-input>
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="loginInfo.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="check_box">
          <el-checkbox v-model="checked">7天免登录</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('loginInfo')">登录</el-button>
        </el-form-item>
      </el-form>
      <el-link to="register">邮箱登录</el-link>

    </el-card>
  </div>
</template>

<script>
import CryptoJS  from 'crypto-js'
import md5 from 'md5'
export default {
  data() {
    return {
      checked: false,
      loginInfo: {
        account: 'test',
        password: '123456',
      },
      rules: {
        account: [{ required: true, message: '请输入账号', trigger: ['blur', 'change'] }],
        password: [{ required: true, message: '请输入密码', trigger: ['blur', 'change'] }]
      }
    }
  },
  created(){
    // console.log(CryptoJS.MD5('hello world').toString())
    // console.log(md5('123456'))
  },
  methods: {
    // 提交
    submitForm(formName) {
      this.useLogin()
    },
    // 登录
    useLogin() {
      let data = {
        email: this.loginInfo.account,
       password: md5(this.loginInfo.password), // 加密
      }
      this.$api.httpLogin(data).then((res) => {
        if (res.code == 200) {
          //   登录成功
          this.$message.success(res.message)
          this.setLoginSession(res.data.token,res.data.user)
          this.$router.push('/')
        } else {
          // this.$message.warning(res.message)
        }
      })
    },
    // 存储登录状态
    setLoginSession(token,user) {
      // 细节未完善！！
      this.$storage.setSession('token', token)
      this.$store.commit('login', {
        userId: user.userId,
        name: user.name,
        email: user.email,
        admin: user.admin,
      })
    },
    // 七天免登录
    freeLogin() {

    }
  },
}
</script>

<style lang="scss" scoped>
.login {
  background-color: rgb(10, 4, 31);
  width: 100vw;
  height: 100vh;

  .check_box {
    margin-bottom: 18px;

    ::v-deep .el-form-item__content {
      line-height: 0;
    }
  }

  .login_title {
    text-align: center;
    padding-bottom: 36px;
  }

  .login_card {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 480px;
    padding: 18px;
  }
}
</style>
